/* BeerScreen, Copyright (C) 2010 Gabriel Tremblay <initnull@gmail.com>
 * http://www.etrange.ca/beerscreen
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ca.etrange.beerscreen.view;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;

import ca.etrange.beerscreen.model.Beer;

public class KegWidget extends JPanel implements Observer {
	
	/* Consts related to keg skin */
	private final static int KEG_PX_BORDER = 1;
	private final static int KEG_PX_TOP = 17;
	private final static int KEG_PX_CONTENT = 100;
	
	private static final long serialVersionUID = -2122689359423967193L;
	private final ImageIcon kegImage;
	private final JLabel titleLabel = new JLabel();
	private final JLabel levelLabel = new JLabel();
	private final JLabel kegImageLabel = new JLabel();
	
	private int kegCapacityMl = 18900;
	private int kegLeveLMl;

	public KegWidget(final Beer beer){
		this.setOpaque(false);
		// load internal keg image
		kegImage = new ImageIcon("skin/keg.png");
		kegImageLabel.setIcon(kegImage);
		kegImageLabel.setAlignmentX(CENTER_ALIGNMENT);
		
		// Add box layout
		final BoxLayout boxLayout = new BoxLayout(this, BoxLayout.Y_AXIS);
		this.setLayout(boxLayout);
		titleLabel.setAlignmentX(CENTER_ALIGNMENT);
		levelLabel.setAlignmentX(CENTER_ALIGNMENT);
		
		titleLabel.setHorizontalAlignment(JLabel.CENTER);
		levelLabel.setHorizontalAlignment(JLabel.CENTER);
		titleLabel.setHorizontalTextPosition(JLabel.CENTER);
		levelLabel.setHorizontalTextPosition(JLabel.CENTER);
		
		// Default values
		this.setTitle("Keg Level");
		this.setKegLeveLMl(0);
		this.setTitleColor(Color.WHITE);
		this.setLevelColor(Color.WHITE);

		this.add(titleLabel);
		this.add(kegImageLabel);
		this.add(levelLabel);
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		// Switch to raphics2D and enable LCD antialiasing
		Graphics2D graphics2D = (Graphics2D)g;
		graphics2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                (RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB));
		
		// Draw keg level
		// Get the keg X and Y position
		final Point kegLocation = kegImageLabel.getLocation();
		
		// liquid
		// Green 100% to 40%, yellow 40% to 20%, red 20% to 0% 
		final int beerLevel = this.getBeerKegLevelPercentage();

		if(beerLevel >= 40){
			graphics2D.setColor(Color.GREEN);
		} else if (beerLevel >= 20){
			graphics2D.setColor(Color.YELLOW);
		} else {
			graphics2D.setColor(Color.RED);
		}
		
		graphics2D.fillRect(
				kegLocation.x + KEG_PX_BORDER, 
				kegLocation.y + KEG_PX_TOP + (KEG_PX_CONTENT - beerLevel), 
				kegImageLabel.getWidth() - (2 * KEG_PX_BORDER), 
				beerLevel);

		super.paintComponent(graphics2D);
	}

	private int getBeerKegLevelPercentage(){
		float tmpPer = ((float)kegLeveLMl / (float)kegCapacityMl) * 100;
		tmpPer = Math.round(tmpPer);
		return (int)tmpPer;
	}
	
	@Override
	public void update(Observable o, Object arg) {
		// TODO Auto-generated method stub
	}
	
	public void setTitle(final String title){
		titleLabel.setText(title);
	}
	
	public void setLevelText(final String level){
		levelLabel.setText(level);
	}
	
	public void setTitleColor(final Color color){
		titleLabel.setForeground(color);
	}
	
	public void setTitleFont(final Font font){
		titleLabel.setFont(font);
	}
	
	public void setLevelColor(final Color color){
		levelLabel.setForeground(color);
	}
	
	public void setLevelFont(final Font font){
		levelLabel.setFont(font);
	}

	public void setKegCapacityMl(int kegCapacityMl) {
		this.kegCapacityMl = kegCapacityMl;
	}

	public void setKegLeveLMl(int kegLeveLMl) {
		this.kegLeveLMl = kegLeveLMl;
		this.setLevelText(kegLeveLMl + "ml");
	}
	

}
